﻿using System.Text.Json;
using WorldmeServer.Models.Base;
using WorldmeServer.Models.配置相关;

namespace WorldmeServer.Utils
{
    public static class ConfigUtils
    {
        /// <summary>
        /// 系统相关设置
        /// </summary>
        public static ApplicationConfig AppConfig { get; private set; }
        /// <summary>
        /// 数据库相关设置
        /// </summary>
        public static DatabaseConfig DatabaseConfig { get; private set; }
        /// <summary>
        /// 邮箱相关设置
        /// </summary>
        public static EmailConfig EmailConfig { get; private set; }

        /// <summary>
        /// 存储所有的消息模板
        /// </summary>
        public static Dictionary<string, Template> Templates = new Dictionary<string, Template>();


        public static void Initialize()
        {
            AppConfig = JsonSerializer.Deserialize<ApplicationConfig>(File.ReadAllText(Constant.PATH_CONFIG_APPLICATION))!;
            DatabaseConfig = JsonSerializer.Deserialize<DatabaseConfig>(File.ReadAllText(Constant.PATH_CONFIG_DATABASE))!;
            EmailConfig = JsonSerializer.Deserialize<EmailConfig>(File.ReadAllText(Constant.PATH_CONFIG_EMAIL))!;

            DirectoryInfo directory = new DirectoryInfo(Constant.PATH_CONFIG_TEMPLATE);
            FileInfo[] templates = directory.GetFiles();

            foreach (FileInfo template in templates)
            {
                string json = File.ReadAllText(template.FullName);
                Template temp = JsonSerializer.Deserialize<Template>(json);
                Templates.Add(temp.Name, temp);
            }
        }


    }
}
